package mo.tags.stack;

import java.util.Stack;

public class L769 {

    public int maxChunksToSorted(int[] arr) {
        Stack<Integer> stack = new Stack<>();
        for (int e : arr) {
            if (stack.isEmpty() || e > stack.peek()) {
                stack.push(e);
            } else {
                int mx = stack.pop();
                while (!stack.isEmpty() && stack.peek() > e) {
                    stack.pop();
                }
                stack.push(mx);
            }
        }
        return stack.size();
    }

}
